<script setup>
import VChart from 'vue-echarts'
import {ref} from "vue";
import * as echarts from "echarts";
import {http} from "@/api";
const option1 = ref(  {
  backgroundColor: "#FFF",
  grid: {
    top: "10",
    bottom: "5",
    left: "0",
    right: "0",
    containLabel: true
  },
  tooltip: {
    trigger: "axis",
    label: {
      show: true,
    },
  },
  xAxis: {
    boundaryGap: true, //默认，坐标轴留白策略
    axisLine: {
      show: false,
    },
    splitLine: {
      show: false,
    },
    axisTick: {
      show: false,
      alignWithLabel: true,
    },
    data: [

    ],
  },
  yAxis: {
    axisLine: {
      show: false,
    },
    splitLine: {
      show: true,
      lineStyle: {
        type: "dashed",
        color: "rgba(33,148,246,0.2)",
      },
    },
    axisTick: {
      show: false,
    },
    splitArea: {
      show: false,
      areaStyle: {
        color: "rgb(245,250,254)",
      },
    },
  },
  series: [
    {
      type: "line",
      symbol: "circle",
      symbolSize: 2,
      smooth: true,
      lineStyle: {
        color: "rgb(33,148,246)",
        shadowBlur: 12,
        shadowColor: "rgb(33,148,246,0.3)",
        shadowOffsetX: 1,
        shadowOffsetY: 1,
      },
      itemStyle: {
        color: "rgb(33,148,246)",
        borderWidth: 1,
        borderColor: "#FFF",
      },
      data: [

      ],
    },
  ],
})
const bufferValue = ref()
const bufferTime = ref()
const baseDetail = ref({
  // 全场额定功率
  rated_power: '',
  // 全场额定容量
  rated_capacity: '',
  // 站用变用电量
  station_electricity: '',
  // 35Kv电缆温度
  cable_temperature: '',
})

const itemList = ref([
  {
    code:  'max_charge_power',
    description: '当前最大可充功率',
    value: '',
    unit: ''
  },
  {
    code:  'max_discharge_power',
    description: '当前最大可放功率',
    value: '',
    unit: ''
  },
  {
    code:  'max_charge_capacity',
    description: '当前最大可充电量',
    value: '',
    unit: ''
  },
  {
    code:  'max_discharge_capacity',
    description: '当前最大可放电量',
    value: '',
    unit: ''
  },
  {
    code:  'rated_power_charge_time',
    description: '额定功率充电时间',
    value: '',
    unit: ''
  },
  {
    code:  'rated_power_discharge_time',
    description: '额定功率放电时间',
    value: '',
    unit: ''
  },
  {
    code:  'today_charge_capacity',
    description: '今日充电量',
    value: '',
    unit: ''
  },
  {
    code:  'today_discharge_capacity',
    description: '今日放电量',
    value: '',
    unit: ''
  },
  {
    code:  'total_charge_capacity',
    description: '累计充电量',
    value: '',
    unit: ''
  },
  {
    code:  'total_discharge_capacity',
    description: '累计放电量',
    value: '',
    unit: ''
  }

])
const option2 = ref( {
  series: [
    {
      name: "有功功率",
      type: "gauge",
      startAngle: 180,
      endAngle: 0,
      detail: {
        formatter: "{value} MW",
        textStyle: {
          fontSize: 24,
          top: 10
        }
      },
      data: [
        {
          value: 50,
          name: "有功功率",
        },
      ],
      axisLine: {
        show: true,
        lineStyle: {
          color: [
            [
              1,
              new echarts.graphic.LinearGradient(0, 0, 1, 0, [
                {
                  offset: 0.1,
                  color: "#FFC600",
                },
                {
                  offset: 0.6,
                  color: "#30D27C",
                },
                {
                  offset: 1,
                  color: "#0B95FF",
                },
              ]),
            ],
          ],
        },
      },
    },
  ],
})
const option3 = ref( {
  series: [
    {
      name: "有功功率",
      type: "gauge",
      startAngle: 180,
      endAngle: 0,
      min: 0,
      max: 10,
      detail: {
        formatter: "{value} MVar",
        textStyle: {
          fontSize: 24,
          top: 10
        }
      },
      data: [
        {
          value:0.23,
          name: "无功功率",
        },
      ],
      axisLine: {
        show: true,
        lineStyle: {
          color: [
            [
              1,
              new echarts.graphic.LinearGradient(0, 0, 1, 0, [
                {
                  offset: 0.1,
                  color: "#FFC600",
                },
                {
                  offset: 0.6,
                  color: "#30D27C",
                },
                {
                  offset: 1,
                  color: "#0B95FF",
                },
              ]),
            ],
          ],
        },
      },
    },
  ],
})
http.energyTimeData.list().then(res => {
  const {data} = res
  option1.value.xAxis.data = data.map(item => item.name.split(' ').join('\n'))
  option1.value.series[0].data = data.map(item => item.value)
})
http.energyData.list().then(res => {
  const {data} = res
  const obj = {}
  const unit = {}
  data.forEach(item => {
    obj[item.code] = item.value
    unit[item.code] = item.unit
  })
  bufferValue.value = obj.battery_remaining_charge + '%'
  bufferTime.value = obj.battery_remaining_time

  for (const baseDetailKey in baseDetail.value) {
    baseDetail.value[baseDetailKey] = obj[baseDetailKey]
  }

  itemList.value.forEach(el => {
    el.value = obj[el.code]
    el.unit = unit[el.code]
  })
  option2.value.series[0].data[0].value = obj.active_power
  option3.value.series[0].data[0].value = obj.inactive_power
})
</script>

<template>
  <el-main>
    <el-row :gutter="20" style="margin-bottom: 20px;height: 260px">
      <el-col :span="12">
        <el-card shadow="never"  style="height: 100%">
          <template #header>
            <div class="card-header">
              <span>基本信息</span>
            </div>
          </template>
          <el-row :gutter="12" style="margin-bottom: 20px">
            <el-col :span="12">
              <el-statistic title="全场额定功率" :value="baseDetail.rated_power" >
                <template #suffix>
                  <span class="unit">MW</span>
                </template>
              </el-statistic>
            </el-col>
            <el-col :span="12">
              <el-statistic title="全场额定容量" :value="baseDetail.rated_capacity" >
                <template #suffix>
                  <span class="unit">MWh</span>
                </template>
              </el-statistic>
            </el-col>
          </el-row>
          <el-row :gutter="12">
            <el-col :span="12">
              <el-statistic title="站用变用电量" :value="baseDetail.station_electricity" >
                <template #suffix>
                  <span class="unit">万kwh</span>
                </template>
              </el-statistic>
            </el-col>
            <el-col :span="12">
              <el-statistic title="35Kv电缆温度" :value="baseDetail.cable_temperature" >
                <template #suffix>
                  <span class="unit">度</span>
                </template>
              </el-statistic>
            </el-col>
          </el-row>
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card shadow="never" style="height: 100%">
          <template #header>
            <div class="card-header">
              <span>电量曲线</span>
            </div>
          </template>
          <v-chart ref="echartsRef2" class="chart" :option="option1" autoresize />
        </el-card>
      </el-col>
    </el-row>
    <el-row :gutter="20" style="height: 600px">
      <el-col :span="16" >
        <el-card shadow="never" style="height: 100%;">
          <template #header>
            <div class="card-header">
              <span>电池信息</span>
            </div>
          </template>
          <div class="f-r" style="width: 100%;height: 100%">
            <div class="f-s f-c a-c j-c">
              <div class="buffer">
                <div class="buffer-value">{{bufferValue }}</div>
                <div class="buffer-icon"></div>
                <div class="buffer-content" :style="{ height: bufferValue }"></div>
              </div>
              <div class="buffer-total f-c j-c a-c">
                <span class="buffer-name">满充剩余时间</span>
                <span class="buffer-time">{{ bufferTime }} <span class="unit">min</span></span>
              </div>
            </div>
            <div class="f-g f-r a-c" style="padding-left: 100px">
              <el-row :gutter="12" style="margin-bottom: 20px">
                <el-col :span="12" v-for="item in itemList">
                  <el-statistic :title="item.description" :value="item.value" style="margin: 20px 0">
                    <template #suffix>
                      <span class="unit">{{ item.unit }}</span>
                    </template>
                  </el-statistic>
                </el-col>
              </el-row>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="8" class="f-c" >
        <div style="height: 50%;padding-bottom: 20px">
          <el-card shadow="never" style="height: 100%;">
            <template #header>
              <div class="card-header">
                <span>PCS实时功率</span>
              </div>
            </template>
            <v-chart class="chart chart2" :option="option2" autoresize />

          </el-card>
        </div>
        <div style="height: 50%">
          <el-card shadow="never" style="height: 100%;">
            <template #header>
              <div class="card-header">
                <span>PCS实时功率</span>
              </div>
            </template>
            <v-chart class="chart chart2" :option="option3" autoresize />

          </el-card>
        </div>
      </el-col>
    </el-row>
  </el-main>
</template>

<style  lang="scss">
.el-card {
  display: flex;
  flex-direction: column;
  position: relative;
  .el-card__body {
    flex-grow: 1;
  }
}
.chart {
  height: 100%;
}
.chart2{
  position: absolute;
  left: 0;
  top: 50px;
  width: 100%;
  height: 110%;
}
.buffer{
  width: 120px;
  height: 300px;
  position: relative;
  border: 3px solid cadetblue;
  margin: 20px 50px;
  .buffer-value {
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    bottom: 0;
    line-height: 300px;
    text-align: center;
    font-size: 28px;
    color: #0bcbb9;
    font-weight: bold;
    z-index: 99;
  }
  .buffer-content {
    width: 100%;
    position: absolute;
    left: 0;
    bottom: 0;
    height: 0;
    background-color:cadetblue ;
    transition: all 0.8s;
  }
  .buffer-icon {
    width: 40px;
    height: 15px;
    background-color:cadetblue ;
    position: absolute;
    left: 50%;
    top: -15px;
    transform: translateX(-50%);
  }
}
.buffer-total {
}

.unit {
  color: #0bcbb9;
}
</style>